package com.example.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;

import com.example.pages.LoginPage;
import com.sun.org.apache.bcel.internal.generic.RETURN;

public class HW_22 {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://localhost/";
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  }

  @Test
  public void testHW22() throws Exception {
    goToMainPage();
    userName("admin", "admin");
    goToMyprofile();
    assertTrue(driver.findElements(By.id("form")).size() >0);
    newUser("ya","ya@ya.ru","123456","123456");
    logOut();
    assertTrue(driver.findElements(By.id("loginform")).size() >0);
    userName("rabota1", "rabota1");
    logOut();
    assertTrue(driver.findElements(By.id("loginform")).size() >0);
  }

private void goToMyprofile() {
	driver.findElement(By.linkText("My profile")).click();
}

private void logOut() {
	driver.findElement(By.linkText("Log out")).click();
	assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to log out[\\s\\S]$"));
}

private void newUser(String nameNewUser, String emailNewUser, String password1, String password2) {
	driver.findElement(By.linkText("User management")).click();
	driver.findElement(By.id("username")).clear();
    driver.findElement(By.id("username")).sendKeys(nameNewUser);
    driver.findElement(By.name("email")).clear();
    driver.findElement(By.name("email")).sendKeys(emailNewUser);
    driver.findElement(By.id("password")).clear();
    driver.findElement(By.id("password")).sendKeys(password1);
    driver.findElement(By.id("password2")).clear();
    driver.findElement(By.id("password2")).sendKeys(password2);
    driver.findElement(By.name("submit")).click();
}

private void chengeEmail() {
	goToMyprofile();
    driver.findElement(By.id("email")).clear();
    driver.findElement(By.id("email")).sendKeys("admin@ya.com");
    driver.findElement(By.name("submit")).click();
}

private void userName(String userName, String password) {
	driver.findElement(By.id("username")).clear();
    driver.findElement(By.id("username")).sendKeys(userName);
    driver.findElement(By.name("password")).clear();
    driver.findElement(By.name("password")).sendKeys(password);
    driver.findElement(By.name("submit")).click();
}

private LoginPage goToMainPage() {
	driver.get(baseUrl + "/php4dvd/");
	return PageFactory.initElements(driver, LoginPage.class);
}

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alert.getText();
    } finally {
      acceptNextAlert = true;
    }
  }
}
